package com.aiju.widgit;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.v4.view.ViewPager;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiju.base.R;

import java.util.List;

/**
 * @version V1.0
 * @FileName: com.aiju.weidiget.IndicatorBar.java
 * @author: 佐为
 * @date: 2016-09-06 13:32
 * @Description ViewPager滑动指示器
 */
public class DotView extends LinearLayout implements ViewPager.OnPageChangeListener {

    private int mVisibleCount = DEFAULT_VISIBLE_COUNT;
    private static final int DEFAULT_VISIBLE_COUNT = 4;
    private static final int DEFAULT_INDICATOR_COLOR = 0xaaff0000;
    private static final int DEFAULT_TEXT_COLOR = 0xaaffffff;
    private static final int DEFAULT_TEXT_SIZE = 16;
    private static final int DEFAULT_SELECT_TEXT_COLOR = 0xffffffff;
    private ViewPager mViewPager;
    private Paint mPaint;
    private float mIndiHeight = 10;
    private int mIndicatorColor;
    private int mTextColor;
    private float mTextSize;
    private int mTextSelectColor;

    private int mInitPosition = 0;

    private onPageChange mPageChange;

    public void setPageChange(onPageChange onPageChange) {
        this.mPageChange = onPageChange;
    }

    public interface onPageChange {
        public void onPageChange(int position);
    }

    public DotView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DotView(Context context) {
        this(context, null);
    }

    public DotView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray ta = context.obtainStyledAttributes(attrs,
                R.styleable.DotView);
        mVisibleCount = ta.getInt(R.styleable.DotView_visible_count,
                DEFAULT_VISIBLE_COUNT);
        mTextSize = ta.getDimension(R.styleable.DotView_text_size,
                DEFAULT_TEXT_SIZE);
        mIndicatorColor = ta.getColor(R.styleable.DotView_indicator_color,
                DEFAULT_INDICATOR_COLOR);
        mTextColor = ta.getColor(R.styleable.DotView_text_color,
                DEFAULT_INDICATOR_COLOR);
        mTextSelectColor = ta.getColor(
                R.styleable.DotView_text_select_color,
                DEFAULT_SELECT_TEXT_COLOR);
        mIndiHeight = ta.getDimension(R.styleable.DotView_indicator_height, mIndiHeight);
        ta.recycle();
        init();
    }

    private void init() {
        mPaint = new Paint();
        // 设置为水平排列
        this.setOrientation(LinearLayout.HORIZONTAL);
    }

    public void setViewPager(ViewPager viewPager) {
        this.mViewPager = viewPager;
        mViewPager.addOnPageChangeListener(this);
        changeSelectTextColor(0);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        // 平移画布
        canvas.translate(0, getHeight());
        mPaint.setColor(mIndicatorColor);
        canvas.drawRect(mInitPosition, -mIndiHeight, mInitPosition
                + getScreenWidth() / mVisibleCount, 0, mPaint);
        canvas.restore();
    }

    public void setTitles(List<String> titles) {
        if (titles != null && titles.size() > 0) {
            for (int i = 0; i < titles.size(); i++) {
                TextView tv = new TextView(getContext());
                TextPaint paint = tv.getPaint();
                paint.setTextSize(mTextSize);
                tv.setTextColor(DEFAULT_TEXT_COLOR);
                tv.setText(titles.get(i));
                tv.setGravity(Gravity.CENTER);
                LayoutParams lp = new LayoutParams(getScreenWidth() / mVisibleCount, LayoutParams.MATCH_PARENT);
                tv.setLayoutParams(lp);
                final int j = i;
                tv.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        mViewPager.setCurrentItem(j);
                    }
                });
                this.addView(tv);
            }
        }
    }

    public int getScreenWidth() {
        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        mInitPosition = (int) ((getScreenWidth() / mVisibleCount) * (position + positionOffset));
        invalidate();
        if (position >= mVisibleCount - 2 && positionOffset > 0 && position < (getChildCount() - 2)) {
            scrollTo((int) ((position - mVisibleCount + 2 + positionOffset) * (getScreenWidth() / mVisibleCount)), 0);
        }
    }

    @Override
    public void onPageSelected(int position) {
        changeSelectTextColor(position);
        if (mPageChange != null) {
            mPageChange.onPageChange(position);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private void changeSelectTextColor(int position) {
        for (int i = 0; i < getChildCount(); i++) {
            TextView tv = (TextView) getChildAt(i);
            int color = mTextColor;
            if (i == position) {
                color = mTextSelectColor;
            }
            tv.setTextColor(color);
        }

    }

}

